unit cad_Metas;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  Formulario_Padrao, StdCtrls, Grids, Spin, RDprint, ExtCtrls, ComCtrls,
  Buttons, db, dbtables, TFlatButtonUnit;

type
  TfrmCad_Metas = class(Tfrm_padr)
    edAno: TSpinEdit;
    sg: TStringGrid;
    btn_incluir: TBitBtn;
    Label3: TLabel;
    Label1: TLabel;
    lblRepre: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure btn_incluirClick(Sender: TObject);
    procedure btnCancelarClick(Sender: TObject);
    procedure sgKeyPress(Sender: TObject; var Key: Char);
    procedure sgSelectCell(Sender: TObject; ACol, ARow: Integer;
      var CanSelect: Boolean);
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure btnGravarClick(Sender: TObject);
    procedure Panel1Exit(Sender: TObject);
  private
    { Private declarations }
    function Valor(p_frase: string): string;
  public
    xRepr: string;
    { Public declarations }
  end;

var
  frmCad_Metas: TfrmCad_Metas;
var
  c, l, lin: integer;

implementation

uses clsRepresQuota, Biblio, Dm;

var
  Meta: TRepresQuota;
  xTabCompl: Boolean;

{$R *.DFM}

procedure TfrmCad_Metas.FormActivate(Sender: TObject);
var
  tq: TQuery;
  y, x: Integer;
  ano, mes: string;
  first: Boolean;
  year, month, day: Word;
begin
  inherited;
  edano.SetFocus;
  DecodeDate(date(), year, month, day);
  Panel1.Enabled := True;
  Panel2.Enabled := true;
  edAno.MinValue := 2005;
  edAno.Value := year;
  sg.cells[00, 0] := 'Ano/Mes';
  sg.cells[01, 0] := 'JAN';
  sg.cells[02, 0] := 'FEV';
  sg.cells[03, 0] := 'MAR';
  sg.cells[04, 0] := 'ABR';
  sg.cells[05, 0] := 'MAI';
  sg.cells[06, 0] := 'JUN';
  sg.cells[07, 0] := 'JUL';
  sg.cells[08, 0] := 'AGO';
  sg.cells[09, 0] := 'SET';
  sg.cells[10, 0] := 'OUT';
  sg.cells[11, 0] := 'NOV';
  sg.cells[12, 0] := 'DEZ';
  sg.cells[00, 1] := IntToStr(edAno.MinValue);

  tq := Tquery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  Meta.Consultar(tq, xRepr);
  if not xTabCompl then begin
    if not tq.Eof then begin
      ano := copy(tq.FieldByName('Ano_Mes').AsString, 1, 4);
      y := 1;
      first := True;

      while not tq.Eof do begin
        if not first then begin
          sg.RowCount := sg.RowCount + 1;
          inc(y);
          First := True;
        end;
        x := StrToInt(copy(tq.FieldByName('Ano_Mes').AsString, 5, 2));
        sg.Cells[x, y] := FloatToStr(tq.FieldByName('Valor').AsFloat);
        sg.Cells[0, y] := copy(tq.FieldByName('Ano_Mes').AsString, 1, 4);
        tq.Next;
        if ano <> copy(tq.FieldByName('Ano_Mes').AsString, 1, 4) then
          First := False;
        ano := copy(tq.FieldByName('Ano_Mes').AsString, 1, 4);
      end;
    end;
  end;
  tq.Close;
  tq.Free;
  xTabCompl := true;
end;

procedure TfrmCad_Metas.btn_incluirClick(Sender: TObject);
var
  i: integer;
  existe: boolean;
begin
  inherited;
  existe := false;
  for i := 1 to sg.rowcount - 1 do begin
    if sg.cells[0, i] = IntToStr(edAno.Value) then
      existe := true;
  end;
  if not existe then begin
    sg.RowCount := sg.RowCount + 1;
    sg.Cells[00, sg.RowCount - 1] := IntToStr(edAno.Value);
  end;
end;

procedure TfrmCad_Metas.btnCancelarClick(Sender: TObject);
begin
  inherited;
  FormActivate(Sender);
end;

procedure TfrmCad_Metas.sgKeyPress(Sender: TObject; var Key: Char);
begin
  inherited;
  if key = '.' then key := ',';
  if key >= ':' then abort;
  if (key >= ' ') and (key <> ',') and (key <> '.') and (key <= '/') then abort;
  btnGravar.enabled := true;
end;

procedure TfrmCad_Metas.sgSelectCell(Sender: TObject; ACol, ARow: Integer;
  var CanSelect: Boolean);
begin
  inherited;
  l := ARow;
  c := ACOl;
  {if (ARow=lin) and (Acol > 0) then begin
    sg.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goEditing,goTabs,goAlwaysShowEditor];
    if acol > 1 then begin
      if (acol mod 2)<>0 then
        sg.cells[acol-1,arow] := '     '+strzero(trim(sg.cells[acol-1,arow]),2);

      if (acol mod 2)=0 then
        sg.cells[acol-1,arow] := Format_vlr2(strtofloat(Valor(sg.cells[acol-1,arow])));
    end;
    if acol=1 then
      sg.cells[24,arow] := '     '+strzero(trim(sg.cells[24,arow]),2);
  end else
    sg.Options := [goFixedVertLine,goFixedHorzLine,goVertLine,goHorzLine,goTabs]; }
end;

procedure TfrmCad_Metas.FormCreate(Sender: TObject);
begin
  inherited;
  Meta := TRepresQuota.Create;
  xTabCompl := false;
end;

procedure TfrmCad_Metas.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  inherited;
  Meta.Free;
end;

procedure TfrmCad_Metas.btnGravarClick(Sender: TObject);
var
  x, y: Integer;
  mes: string;
  ano: string;
begin
  inherited;
  for y := 1 to sg.RowCount - 1 do begin
    x := 1;
    while x < sg.ColCount do begin
      if sg.cells[x, 0] = 'JAN' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'FEV' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'MAR' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'ABR' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'MAI' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'JUN' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'JUL' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'AGO' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'SET' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'OUT' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'NOV' then mes := StrZero(x, 2);
      if sg.cells[x, 0] = 'DEZ' then mes := StrZero(x, 2);

      ano := sg.Cells[0, y];

      if sg.Cells[x, y] <> '' then
        Meta.Valor := StrToFloat(sg.Cells[x, y])
      else
        Meta.Valor := 0;
      try
        Meta.Gravar(xRepr, ano + mes);
      except
        ShowMessage('Erro ao gravar a quota mensal');
      end;
      inc(x);
    end; //fim while
  end; //fim do for
end;

function TfrmCad_Metas.Valor(p_frase: string): string;
var
  cont: Integer;
  c: string;
  d: string;
begin
  d := '';
  for cont := 1 to length(p_frase) do begin
    c := Copy(p_frase, cont, 1);
    if (c <> '.') and (c <> '-') and (c <> ' ') and (c <> '/') and (c <> '\') then
      d := d + c;
  end;
  result := d;

end;

procedure TfrmCad_Metas.Panel1Exit(Sender: TObject);
begin
  inherited;
  if not btnFechar.Focused then
    sg.SetFocus;
end;

end.
